<?php

namespace App\Http\Controllers\CRM;

use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\CRM\AssistantTypeRequest;
use App\Http\Resources\CRM\AssistantTypeResource;
use App\Models\AssistantType;
use Illuminate\Http\Request;

class AssistantTypeController extends Controller
{
    public function getList()
    {
        $types = AssistantType::oldest('sort')->get();

        return AssistantTypeResource::collection($types);
    }

    public function getInfo(AssistantType $type)
    {
        return new AssistantTypeResource($type);
    }

    public function create(AssistantTypeRequest $request)
    {
        $type = AssistantType::create($request->only([
            'name', 'sort'
        ]));

        return new AssistantTypeResource($type);
    }

    public function edit(AssistantType $type, AssistantTypeRequest $request)
    {
        $type->update($request->only([
            'name', 'sort'
        ]));

        return new AssistantTypeResource($type);
    }

    public function remove(AssistantType $type)
    {
        if ($type->assistants()->exists()) {
            return ApiResponseHelper::json([], 400,'该类型内存在助教，无法删除');
        }

        $type->delete();

        return ApiResponseHelper::json([]);
    }
}
